//alert(0)
var webApp = function (window, option, Callback) {

    //alert(1)
    option = option || {};
    var vue = {};
    Callback = Callback || function () {};

    var _pwa_sw = require('./sw-drive.js');
    var axios = require('./ajax');
    var notify = require('./notify');

    var Fullscreen = require('./Fullscreen');
    this.setvue = function (val) {
        vue = val;
    };
    //alert(2)
    var newPath = window.location.hash.replace('#/', '').replace('#', '');

    var vue_links = {};
    var getmenulist = function (List, parent) {
        parent = parent || {
            id: "home"
        };
        for (var i = 0; i < List.length; i++) {
            var item = List[i];
            item.parent = parent;
            item.parents = List;
            if (item.subs) {
                item.subs = getmenulist(item.subs, item);
            }
            if (item.index && item.subs == null) {
                vue_links[item.index] = item;
            }
            List[i] = item;
        }
        return List;
    }


    this.get = function (self, callback) {
        //alert(3)
        this.setvue(self);
        notify(self);
        var Axios = axios(self);
        self.$Ajax.setAxios(Axios);

        self.$UserAgent.isMobile = true;
        self.$WSPools.message("remote-login", function (db) {
            self.$message.error(self.$getTitle("@sys_remote_landing"));
            self.$router.push("/login");
        });
        Callback(self);

       /* window.addEventListener('DOMContentLoaded', function () {
            if (self.$UserAgent.isTouch && !Fullscreen()) {
                Fullscreen(self)
            }
        })*/
        // self.$defaultPath('/home');
        //alert(4)
        var value = function (window, document, self) {
            var userjurisdictions = [];
            var language = '<%language%>';
            language = language || {};
            var displayMode = Math.min(window.innerWidth, window.innerWidth) >= 768 ? 1 : 2;
            var Drive = function () {
                this.domain = "<%domain%>";
                this.language = language;
                this.drive = {
                    item: "<%extend%>",
                    main: this.mainDrive
                }
            }

            Drive.prototype = {
                openMenu: function (callback) {
                    var D = this;
                    self.$Ajax(self.$Uris.Monitor, {})
                        .then(function (response) {
                            if (response.data) {
                                var db = response.data;
                                if (db.error > 0) {
                                    self.$Loading.hide();
                                    self.$router.push('/login');
                                } else {
                                    callback(db);

                                }
                            }
                        })
                },
                mainDrive: function (callback) {
                    self.$UserJurisdiction.get(function (db) {
                        _Drive.openMenu(function (db) {
                            if (db.socket_id) {
                                self.$WSPools.init({
                                    socket_id: db.socket_id
                                });
                            }
                            userjurisdictions = db.jurisdictions;
                            var List = getmenulist(userjurisdictions);
                            self.UIEventPool.run("Sys_SideBar", "setDataList", List);
                        })
                    });
                    _Drive.openMenu(function (db) {
                        _Drive.setval(db, callback)
                    })
                },
                setval: function (obj, callback) {
                    var RightMenu = {
                        InitVal: 0,
                        'visible': true,
                        'icon': 'el-icon-user',
                        Command: function (e) {
                            if (e.value == "RestartBtn") {
                                self.$router.push('/restart');
                            } else if (e.value == "loginout") {
                                self.loadingSwitch = true;
                                self.$Ajax(self.$Uris.Signout, {}).then(function (response) {
                                    if (response.data && response.data.error == 0) {
                                        setTimeout(() => {
                                            self.loadingSwitch = false;

                                            self.$router.push('/login');
                                        }, self.TimeInterval);
                                    } else {
                                        setTimeout(() => {
                                            self.loadingSwitch = false;
                                            self.$router.push('/login');
                                        }, self.TimeInterval);
                                    }
                                })
                            } else if (e.value == 'Close') {
                                window.close();
                            } else if (e.value == 'Fullscreen' || e.value == 'Exit_Fullscreen') {
                                var off = Fullscreen(self);
                                for (var i = 0; i < RightMenu.items.length; i++) {
                                    var element = RightMenu.items[i];
                                    if (element.value == 'Fullscreen') {
                                        element.visible = !off;
                                    } else if (element.value == 'Exit_Fullscreen') {
                                        element.visible = off;
                                        // RightMenu.InitVal++;
                                        return;
                                    }
                                };
                            }
                        },
                        items: [{
                                value: "RestartBtn",
                                label: "@sys_RestartBtn"
                            },
                            {
                                value: "loginout",
                                label: "@sys_loginout"
                            }
                        ].concat((self.$UserAgent.isTouch ? [{
                            value: "Fullscreen",
                            visible: !Fullscreen(),
                            label: "@sys_Fullscreen"
                        }, {
                            value: "Exit_Fullscreen",
                            visible: Fullscreen(),
                            label: "@sys_Exit_Fullscreen"
                        }, {
                            value: "Close",
                            visible: true,
                            label: "@sys_Close"
                        }] : []))
                    };
                    var HomeRightMenu = RightMenu;
                    var transition = {
                        "InitVal": 0,
                        "name": "transition"
                    };

                    userjurisdictions = obj.jurisdictions;
                    var DataList = getmenulist(userjurisdictions);
                    var SelectNodes = [];
                    var Sidebar = {
                        InitVal: 0,
                        rightIcon: document.body.dir == "rtl" ? "el-icon-arrow-left" : "el-icon-arrow-right",
                        "InitVal": 0,
                        "ID": "Sys_SideBar",
                        "Select": function (obj) {
                            if (obj.subs && displayMode == 2) {
                                SelectNodes.push(obj);
                                drawer.title = obj.title;
                                setTimeout(function () {
                                    HOME.left.visible = true;
                                    self.UIEventPool.run("Sys_SideBar", "setDataList", obj.subs);
                                }, 400);
                            } else if (obj.subs == null) {
                                panel.title = obj.title;
                                newPath = obj.index;
                                drawer.InfoFormVisible = false;
                                if (displayMode == 2) {
                                    self.UIEventPool.run("Mobile-Home-System", "Show", {
                                        id: "Transition"
                                    })
                                }
                                panel.title = obj.title;
                            };
                            if (displayMode == 2) {} else {
                                if (obj.index == "notice") {
                                    RightMenu.visible = true;
                                } else {
                                    RightMenu.visible = false;
                                };
                                panel.right = RightMenu;
                            }
                        },
                        "name": "sidebar",
                        "Animating": displayMode == 2,
                        "Parameter": {
                            "data": {
                                "sourceid": "M_732",
                                "id": "0"
                            }
                        },
                        "path": "",
                        displayMode: displayMode,
                        "DataList": DataList,
                        "NotInitialization": false
                    };
                    var panelLeft = null;
                    var panel = {
                        "name": "panel",
                        "title": self.$getMeta('title'),
                        "InitVal": 0,
                        "right": (displayMode == 2 ? {
                            visible: false,
                            'name': "panel-menu",
                            'icon': 'fa fa-bars'
                        } : RightMenu),
                        left: {
                            Click: function (obj) {
                                if (panelLeft && panelLeft.callback) {
                                    var _callback = panelLeft.callback;
                                    panelLeft = null;
                                    _callback(panel.left.Click);
                                    if (displayMode == 2) {} else {
                                        panel.left.icon = "fa fa-bars"
                                    }
                                    return;
                                }
                                if (displayMode == 2) {
                                    self.UIEventPool.run("Mobile-Home-System", "Show", {
                                        id: "Home-Page"
                                    })
                                } else {
                                    drawer.InfoFormVisible = !drawer.InfoFormVisible;
                                }
                            },
                            'name': "panel-menu",
                            'icon': displayMode == 2 ? (document.body.dir == "rtl" ? "el-icon-arrow-right" : "el-icon-arrow-left") : 'fa fa-bars',
                            items: []
                        },
                        "Form": transition
                    };
                    var HOME = {
                        InitVal: 0,
                        "name": "panel",
                        "title": self.$getMeta('title'),
                        "InitVal": 0,
                        left: {
                            Click: function (obj) {
                                var items = null;
                                if (SelectNodes.length > 0) {
                                    var _obj = SelectNodes[SelectNodes.length - 1];

                                    SelectNodes.splice(SelectNodes.length - 1, 1);

                                    if (_obj.callback) {
                                        _obj.callback(HOME.left.Click)
                                        return;
                                    }

                                    items = _obj.parents;
                                    console.log(SelectNodes)
                                    if (SelectNodes.length == 0) {
                                        setTimeout(function () {
                                            HOME.left.visible = false;
                                        }, 100);
                                    }
                                } else {
                                    setTimeout(function () {
                                        HOME.left.visible = false;
                                    }, 300);
                                }
                                if (items) {
                                    self.UIEventPool.run("Sys_SideBar", "setDataList", items);
                                }
                            },
                            visible: false,
                            'name': "panel-menu",
                            'icon': document.body.dir == "rtl" ? "el-icon-arrow-right" : "el-icon-arrow-left",
                        },
                        "right": HomeRightMenu,
                        "Form": Sidebar
                    };
                    var drawer = {
                        InitVal: 0,
                        name: "drawer",
                        appendToBody: false,
                        size: "256px;",
                        withHeader: false,
                        title: self.$getMeta('title'),
                        direction: (language.direction == "rtl" ? "rtl" : "ltr"),
                        InfoFormVisible: false,
                        condition: displayMode == 2 ? {} : Sidebar
                    };
                    var SelectOk = function () {};
                    var FileExplorer = {
                        "name": "panel",
                        "type": "success",
                        "title": "FileExplorer",
                        "InitVal": 0,
                        "left": {
                            'visible': true,
                            'name': "panel-menu",
                            'icon': 'el-icon-check', //'el-icon-arrow-left',
                            "Click": function () {
                                SelectOk();
                                if (displayMode == 2) {
                                    self.UIEventPool.run("Mobile-Home-System", "Show", {
                                        id: "Transition"
                                    })
                                } else {
                                    self.UIEventPool.run("Mobile-Home-System", "Show", {
                                        id: "Home-Page"
                                    })
                                }
                            }
                        },
                        setFileList: function (val) {
                            FileExplorer.Form = val;
                            self.UIEventPool.run("Mobile-Home-System", "Show", {
                                id: "FileExplorer"
                            })
                        },
                        Config: {
                            extends: function (filelist) {
                                SelectOk = filelist._SelectOk;
                            },
                            InitAppTopMenu: function (obj) {
                                FileExplorer.right = obj;
                                return;
                            }
                        },
                        "Form": {}
                    }
                    self.$FileExplorer.init(FileExplorer, self);
                    self.$ShowAppMenu(function () {
                        drawer.InfoFormVisible = true;
                    })
                    self.$InitAppTopMenu(function (val, _panelLeft) {
                        if (_panelLeft && val == null) {
                            if (displayMode == 2) {


                            } else {
                                if (_panelLeft == 1) {
                                    panel.left.icon = "fa fa-bars"
                                } else {
                                    panel.left.icon = document.body.dir == "rtl" ? "el-icon-arrow-right" : "el-icon-arrow-left"
                                }
                            }
                            panelLeft = _panelLeft == 1 ? null : _panelLeft;
                            // SelectNodes.push(leftval)

                            return
                        }
                        panel.right = val;
                    });
                    if (_window.uuid) {
                        var obj = require('./userkey');
                        if (displayMode == 2) {
                            HOME = obj.getMobile();
                            panel = obj.getMobile();
                        } else {
                            panel = obj.getMobile();
                        }
                    }
                    var cf = {
                        "InitVal": 1,
                        "name": "carousel",
                        "ShowArrow": false,
                        "indicatorPosition": "none",
                        "ID": "Mobile-Home-System",
                        "autoplay": false,
                        "type": "-",
                        "initialIndex": 1,
                        "loop": false,
                        "List": {
                            "a1": {
                                "id": "Home",
                                "Form": {}
                            },
                            "a2": {
                                "id": "Home-Page",
                                "Form": {
                                    "InitVal": 0,
                                    "name": "container",
                                    "List": {
                                        drawer: drawer,
                                        "container": {
                                            "InitVal": 0,
                                            "name": "container",
                                            "List": {
                                                "main": {
                                                    "InitVal": 0,
                                                    "name": "main",
                                                    "Form": displayMode == 2 ? HOME : panel
                                                },
                                                "footer": {
                                                    "InitVal": 0,
                                                    "name": "footer",
                                                    "height": "0px",
                                                    "Form": {}
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            "a3": {
                                "id": "Transition",
                                "Form": displayMode == 2 ? panel : {}
                            },
                            "a4": {
                                "id": "FileExplorer",
                                "Form": FileExplorer
                            }
                        }
                    };

                    callback(cf)
                    var openviwe = function (url_file) {
                        setTimeout(() => {
                            var openObject = vue_links[url_file]
                            if (openObject) {
                                Sidebar.Select(openObject)
                            }
                        }, self.TimeInterval);
                    }
                    openviwe(newPath);
                    window.addEventListener('popstate', function (e) {
                        var _newPath = window.location.hash.replace('#/', '').replace('#', '');
                        if (_newPath != newPath) {
                            newPath = _newPath;
                            openviwe(newPath)
                        }
                    });


                }
            }
            var _Drive = new Drive()

            return _Drive;
        };
        if (callback) {
            callback(value)
        } else {
            return value;
        }
    }
    _pwa_sw(window, option.swdrive);

};
module.exports = webApp;